#!/bin/bash

KUBECONFIG_YAML_PATH=/etc/kubernetes/yaml
KUBECONFIG_PATH=/etc/kubernetes/kubeconfig
POD_CIDR=172.16.0.0/16

cat <<EOF | sudo tee $KUBECONFIG_YAML_PATH/kube-proxy-conf.yaml
kind: KubeProxyConfiguration
apiVersion: kubeproxy.config.k8s.io/v1alpha1
clientConnection:
  kubeconfig: $KUBECONFIG_PATH/kube-proxy.kubeconfig
mode: ipvs
clusterCIDR: $POD_CIDR
EOF

cat <<EOF | sudo tee /usr/lib/systemd/system/kube-proxy.service
[Unit]
Description=Kubernetes Kube Proxy
Documentation=https://github.com/kubernetes/kubernetes
After=network.target

[Service]
ExecStart=/usr/local/bin/kube-proxy \\
  --config=$KUBECONFIG_YAML_PATH/kube-proxy-conf.yaml \\
  --v=2

Restart=always
RestartSec=10s

[Install]
WantedBy=multi-user.target
EOF

sudo systemctl daemon-reload
sudo systemctl enable --now kube-proxy